<!--
  Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.

  https://www.mall4j.com/

  未经允许，不可做商业用途！

  版权所有，侵权必究！
-->

<template>
  <!-- 这个是商品列表 -->
  <div class="Mall4j goods_list_component">
    <template v-if="config.dataList && config.dataList.length > 0">
      <template
        v-for="(item, index) in config.dataList"
        :key="index"
      >
        <div
          :style="setGoodsListStyle"
          :class="setClassName"
          @click="handleItem(item)"
        >
          <div class="goods-img-content">
            <el-image
              :src="checkFileUrl(item.imgs)"
              fit="fill"
            >
              <template #error>
                <div class="image-slot">
                  <img
                    src="@/assets/img/def.png"
                    alt
                  >
                </div>
              </template>
            </el-image>
            <!-- 下架商品蒙版 start -->
            <!-- 限时秒活动 -->
            <template v-if="type">
              <div
                v-if="setImgs(item, 'show')"
                class="imgs_shelves"
              >
                <img
                  class="been_imgs"
                  :src="setImgs(item, 'src')"
                  alt
                >
              </div>
            </template>
            <!-- 普通商品下架 -->
            <template v-else>
              <div
                v-if="item.status !== 1"
                class="imgs_shelves"
              >
                <img
                  class="been_imgs"
                  src="@/assets/images/been_shelves.png"
                  alt
                >
              </div>
            </template>
            <!-- 下架商品蒙版 end -->
          </div>
          <div
            v-if="isShowAll"
            class="goods-text"
          >
            <div class="toptext">
              <span
                v-show="config.showName === 0"
                class="name"
              >{{ item.name }}</span>
              <span
                v-show="config.showDescription === 0"
                class="description"
              >{{ item.description }}</span>
            </div>
            <div
              v-show="config.showPrice === 0"
              class="price"
            >
              <div class="count">
                <span>￥</span>
                <span>{{ getPrice(item.price, 'left') }}.</span>
                <span>{{ getPrice(item.price, 'right') }}</span>
              </div>
            </div>
          </div>
        </div>
      </template>
    </template>
    <template v-else>
      <template
        v-for="index in config.listTypeList.length"
        :key="index"
      >
        <div
          :style="setGoodsListStyle"
          :class="setClassName"
        >
          <el-image
            fit="cover"
            style="background: rgba(243, 245, 247, 0.39);"
          >
            <template #error>
              <div class="image-slot">
                <img
                  src="@/assets/img/def.png"
                  alt
                >
              </div>
            </template>
          </el-image>
          <div
            v-if="isShowAll"
            class="goods-text"
          >
            <div class="toptext">
              <span
                v-show="config.showName === 0"
                class="name"
              >商品名称</span>
              <span
                v-show="config.showDescription === 0"
                class="description"
              >商品描述</span>
            </div>
            <div
              v-show="config.showPrice === 0"
              class="price"
            >
              <div class="count">
                <span>￥价格</span>
              </div>
            </div>
          </div>
        </div>
      </template>
    </template>
  </div>
</template>
<script setup>
import { ElMessage } from 'element-plus'
import beenShelvesPng from '@/assets/images/been_shelves.png'
import hasEndPng from '@/assets/images/has_end.png'
import { checkFileUrl } from '@/utils/index.js'

const router = useRouter()

const props = defineProps({
  config: { // 商品配置信息
    type: Object,
    default: () => {}
  },
  type: { // 当前商品是什么类型
    type: String,
    default: () => ''
  }
})
// 得到价格显示
const getPrice = computed(() => {
  return (price, type) => {
    if (!price) return
    const point = price.toString().indexOf('.') // 如果为-1则表示没找到
    let leftPrice
    let rightPrice
    if (point === -1) { // 当前是整数
      leftPrice = price
      rightPrice = '00'
    } else {
      leftPrice = price.toString().slice(0, point)
      rightPrice = price.toString().slice(point + 1)
    }
    switch (type) {
      case 'left':
        return leftPrice
      case 'right':
        return rightPrice
      default:
        break
    }
  }
})

// 设置商品列表样式
const setGoodsListStyle = computed(() => {
  let width = ''
  // 如果当前每行展示三个，此时每个对应的宽度是
  if (props.config.showMany === 3) {
    width = 'calc(33.3% - (20px - (20px / 3)))'
  } else if (props.config.showMany === 4) {
    width = 'calc(25% - (20px - (20px / 4)))'
  } else if (props.config.showMany === 5) {
    width = 'calc(20% - (20px - (20px / 5)))'
  }
  return {
    width
  }
})

// 设置类名
const setClassName = computed(() => {
  let className = 'goods-list-items'
  if (props.config.showMany === 3) {
    className = 'goods-list-items three'
  } else if (props.config.showMany === 4) {
    className = 'goods-list-items four'
  } else if (props.config.showMany === 5) {
    className = 'goods-list-items five'
  }
  return className
})

// 当所有内容不显示的时候
const isShowAll = computed(() => {
  return !(props.config.showName !== 0 && props.config.showDescription !== 0 && props.config.showPrice !== 0)
})

// 活动商品下架或者结束
const setImgs = computed(() => {
  return (item, val) => {
    const params = {
      status: false,
      imgs: beenShelvesPng
    }
    if (props.type == 'skill') { // 秒杀
      if (item.status != 1) { // 当前就显示下架图标
        params.imgs = beenShelvesPng
        params.status = true
      } else if ((item.status === 1 && item.prodType !== 2) || (item.activityStatus !== 1)) { // 显示结束图标
        params.imgs = hasEndPng
        params.status = true
      } else {
        params.status = false
      }
    } else if (props.type == 'discount') { // 拼团
      if (item.status !== 1) { // 当前就显示下架图标
        params.imgs = beenShelvesPng
        params.status = true
      } else if ((item.status === 1 && item.prodType !== 1) || (item.activityStatus !== 1)) { // 显示结束图标
        params.imgs = hasEndPng
        params.status = true
      } else {
        params.status = false
      }
    }
    if (val === 'show') {
      return params.status
    } else if (val === 'src') {
      return params.imgs
    }
  }
})

// 跳转详情
const handleItem = (item) => {
  if (item.status !== 1) {
    return ElMessage.warning('当前商品已下架')
  }
  let newPage
  if (props.type == 'skill') { // 秒杀
    newPage = router.resolve({ path: '/detail', query: { prodId: item.prodId } })
    window.open(newPage.href, '_blank', 'noopener,noreferrer')
  } else {
    newPage = router.resolve({ path: '/detail', query: { prodId: item.id } })

    window.open(newPage.href, '_blank', 'noopener,noreferrer')
  }
  window.open(newPage.href, '_blank', 'noopener,noreferrer')
}

</script>
<style lang="scss" scoped>
@use "index";
</style>
